package cn.com.pajx.pajx_smart_platform.api;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by ruancw on 2023/04/03.
 * 网络请求类
 */

public class HttpApiService {
    private final Retrofit retrofit;

    public static HttpApiService getInstance() {
        return Singleton.instance;
    }

    private static class Singleton {
        private static final HttpApiService instance = new HttpApiService();
    }

    private HttpApiService() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())//解决上传图片带参数添加""的问题（必须放在Gson前）
                .addConverterFactory(GsonConverterFactory.create())//将返回的数据转为Gson
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加Rxjava的回调
                .client(genericClient())
                .build();
    }

    private OkHttpClient genericClient() {
        OkHttpClient httpClient;
        httpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.MINUTES)
                .readTimeout(5, TimeUnit.MINUTES)
                .addInterceptor(new Interceptor() {
                    @NonNull
                    @Override
                    public Response intercept(@NonNull Chain chain) throws IOException {
                        Request request = chain.request();
                        request = request
                                .newBuilder()
                                .build();
                        return chain.proceed(request);
                    }
                })
                .addInterceptor(new HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        return httpClient;
    }

    private ApiService getApiService() {
        return getService(ApiService.class);
    }

    //通过retrofit获取ApiService实例
    private <T> T getService(Class<T> cls) {
        return retrofit.create(cls);
    }

    //获取网络数据
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public Observable getRemoteData(String url, LinkedHashMap<String, String> paramsMap) {
        String data=HttpParamsUtil.paramsToJson(paramsMap);
        return getApiService().getRemoteData(url, HttpParamsUtil.generateBodyJson(data));
    }

    //获取html源码数据
    public Observable getHtmlData(String url, LinkedHashMap<String, String> map) {
        return getApiService().getHtmlData(url, HttpParamsUtil.addSignToMap(map));
    }

    //上传文件
    public Observable updateFile(String url, List<MultipartBody.Part> file, LinkedHashMap<String, String> map) {
        return getApiService().upLoadFile(url, file, HttpParamsUtil.addSignToMap(map));
    }

    //上传文件
    public Observable updatePhoto(String url, MultipartBody.Part file, LinkedHashMap<String, String> map) {
        return getApiService().upLoadPhoto(url, file, HttpParamsUtil.addSignToMap(map));
    }

    //下载文件
    public Observable downloadFile(String url, LinkedHashMap<String, String> map) {
        return getApiService().downloadFile(url, HttpParamsUtil.addSignToMap(map));
    }
}
